package com.shoe.controller;

import com.shoe.pojo.AdminCouponPojo;
import com.shoe.service.ACouponService;
import com.shoe.util.DateConversion;
import com.shoe.util.ResultUtil;
import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
//@MultipartConfig
@RequestMapping("admin")
public class ACouponController {

    @Autowired
    ACouponService aCouponService;

    @Autowired
    ResultUtil resultUtil;

    @GetMapping("selectacoupon")
    public ResponseResult selectACoupon(Integer page, Integer limit, String couponType, Integer couponMoney, Integer couponHolding) {
        List<AdminCouponPojo> adminCoupPage = aCouponService.selectACouByConPage((page - 1) * limit, limit, couponType, couponMoney, couponHolding);
        List<AdminCouponPojo> adminCoupAll = aCouponService.selectACouByConAll(couponType, couponMoney, couponHolding);

        if (null != adminCoupPage && adminCoupPage.size() > 0) {
            return resultUtil.getResponseResult(0, "", adminCoupAll.size(), adminCoupPage);
        } else {
            return resultUtil.getResponseResult(2, "没有结果");
        }

    }

    @GetMapping("updatecou")
    public ResponseResult updataCou(AdminCouponPojo adminCouponPojo) {

        DateConversion dateConversion = new DateConversion();
        adminCouponPojo.setCouponDate(dateConversion.Data2Sting());

        System.out.println(adminCouponPojo.toString());

        int arrRow = aCouponService.updateACou(adminCouponPojo);
        if (arrRow > 0) {
            return resultUtil.getResponseResult(0, "", arrRow);
        } else {
            return resultUtil.getResponseResult(2, "操作失败");
        }
    }

    @GetMapping("delecou")
    public ResponseResult updataCou(Integer couponId) {

        int arrRow = aCouponService.delACou(couponId);
        if (arrRow > 0) {
            return resultUtil.getResponseResult(0, "", arrRow);
        } else {
            return resultUtil.getResponseResult(2, "操作失败");
        }
    }

    @PostMapping("acou")
    public void addACou(HttpServletResponse response, String couMoney, String couHolding, String couponType, @RequestParam("file") MultipartFile file) throws IOException {

        System.out.println(couMoney);
        System.out.println(couHolding);
        System.out.println(couponType);

        String fileName = file.getOriginalFilename();

        String fileSuffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();

        String fileNewName = couMoney + fileSuffix;

        String targetFilePath = "F:\\Git-2.2\\qiguai\\img\\couphoto";

        File myfile = new File(targetFilePath + File.separator + fileNewName);
        file.transferTo(myfile);

        String addACouphoto = "../img/couphoto/" + fileNewName;
        System.out.println(addACouphoto);

        Integer couponMoney = Integer.parseInt(couMoney);
        Integer couponHolding = Integer.parseInt(couHolding);

        AdminCouponPojo addACouInfo = addACouClass(couponMoney, couponHolding, couponType, addACouphoto);

        int arrRow = aCouponService.addACou(addACouInfo);

        if (arrRow > 0) {
            response.sendRedirect("http://localhost:63343/qiguai/acouadd/index.html?0");
        } else {
            response.sendRedirect("http://localhost:63343/qiguai/acouadd/index.html?2");
        }
    }

    @PostMapping("couadderr")
    public void couAddErr(HttpServletResponse response) throws IOException {

        response.sendRedirect("http://localhost:63343/qiguai/acouadd/index.html?3");
    }

    public AdminCouponPojo addACouClass(Integer couponMoney, Integer couponHolding, String couponType, String addACouphoto) {

        DateConversion dateConversion = new DateConversion();

        AdminCouponPojo adminCouponPojo = new AdminCouponPojo();

        adminCouponPojo.setCouponDate(dateConversion.Data2Sting());
        adminCouponPojo.setCouponMoney(couponMoney);
        adminCouponPojo.setCouponHolding(couponHolding);
        adminCouponPojo.setCouponType(couponType);
        adminCouponPojo.setCouponImg(addACouphoto);

        return adminCouponPojo;
    }
}
